package com.itheima.server.controller;

import com.itheima.commons.utils.JwtUtils;
import com.itheima.server.interceptor.UserHolder;
import com.itheima.server.service.UserInfoService;
import com.itheima.tanhua.model.domain.UserInfo;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.security.oauth2.resource.OAuth2ResourceServerProperties;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/user")
public class UserInfoController {

    @Autowired
    private UserInfoService userInfoService;

    /**
     * 保存用户信息
     * @param userInfo
     * @return
     */
    @PostMapping("/loginReginfo")
    public ResponseEntity loginReginfo(@RequestBody UserInfo userInfo) {


        //向接收到的userInfo中设置用户id
        Long userId = UserHolder.getUserId();


        userInfo.setId(Long.valueOf(userId));

        //调用Service完成用户信息保存
        userInfoService.save(userInfo);

        return ResponseEntity.ok(null);
    }

    /**
     * 上传用户头像
     * @param multipartFile
     * @return
     * @throws IOException
     */
    @PostMapping("/loginReginfo/head")
    public ResponseEntity updateHead(MultipartFile multipartFile) throws IOException {

        //向接收到的userInfo中设置用户id
        Long userId = UserHolder.getUserId();

        userInfoService.updateHead(multipartFile,Long.valueOf(userId));

        return ResponseEntity.ok(null);

    }
}
